package com.liyu.controller;

import cn.hutool.core.date.DateUtil;
import com.liyu.config.DlxComment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 李誉
 * @date 2022-07-06 22:49
 * @description
 */
@Slf4j
@RestController
@RequestMapping("dlx")
public class DlxController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("{msg}")
    public void send(@PathVariable("msg") String msg) {
        rabbitTemplate.convertAndSend(DlxComment.NORMAL_EXCHANGE, DlxComment.NORMAL_RK, msg, message -> {
            MessageProperties messageProperties = message.getMessageProperties();
            // 设置十秒过期时间
            messageProperties.setExpiration("10000");
            return message;
        });
        log.info("消息发送成功，交换机：{}，路由：{}，时间：{}，消息：{}", DlxComment.NORMAL_EXCHANGE, DlxComment.NORMAL_RK, DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), msg);
    }

    @GetMapping("maxlength/{msg}")
    public void sendMaxLength(@PathVariable("msg") String msg) {
        for (int i = 1; i <= 10; i ++ ) {
            rabbitTemplate.convertAndSend(DlxComment.NORMAL_EXCHANGE, DlxComment.NORMAL_MAXLENGTH_RK, msg+i, message -> message);
            log.info("消息发送成功，交换机：{}，路由：{}，时间：{}，消息：{}", DlxComment.NORMAL_EXCHANGE, DlxComment.NORMAL_MAXLENGTH_RK, DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), msg + 1);
        }
    }
}
